package com.tutu.starter;

import com.tutu.starter.bean.Car;
import com.tutu.starter.bean.Pet;
import com.tutu.starter.bean.User;
import com.tutu.starter.config.SpringConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@SpringBootApplication
public class SpringbootStarterApplication {

    public static void main(String[] args) {

        //返回IOC容器
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootStarterApplication.class, args);
        //获取容器里所有组件的名称
        /* String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name);
            System.out.println("---------------------------");
        }*/

        //之前学习Spring时，通过xml文件的形式获取bean
        ClassPathXmlApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext("bean.xml");
        User user01 = xmlApplicationContext.getBean("User", User.class);
        Pet pet01 = xmlApplicationContext.getBean("Pet", Pet.class);
        System.out.println("xml文件中的bean对象" + user01);
        System.out.println("xml文件中的bean对象" + pet01);

        //返回config配置类中的bean对象
        User user02 = context.getBean("user", User.class);
        Pet pet02 = context.getBean("pet", Pet.class);
        System.out.println("config配置类中的bean对象" + user02);
        System.out.println("config配置中的bean对象" + pet02);


        //config配置类本身也是bean对象 当proxyBeanMethods属性为true时 为一个代理类对象
        SpringConfig config = context.getBean(SpringConfig.class);
        System.out.println(config);

        //当proxyBeanMethods为true时,返回的bean对象是相同的; 反之,返回的bean对象都是不同的
        User user03 = config.user();
        User user04 = config.user();
        System.out.println(user03 == user04);

        //按照类型查找容器内的组件
        String[] userType = context.getBeanNamesForType(User.class);
        for (String user : userType) {
            System.out.println(user);
        }

        Car car = context.getBean("car", Car.class);
        System.out.println(car);


        boolean user = context.containsBean("user");
        System.out.println("容器中是否存在名称为user的实例" + user);

    }

}


